﻿#ifndef _OI_PLSCANNER_PLCTASK_HPP_
#define _OI_PLSCANNER_PLCTASK_HPP_
#include "Database/OiDatabase.hpp"
#include "Appl/OiAppl.hpp"
#include <QThread>
#include "Util/OiPLC.hpp"
namespace Oi
{
    namespace PLScannerCore
    {

        class  PLCTask : public QThread
        {
            Q_OBJECT

        public:
            enum WriteType
            {
                WT_HEARTBEAT = 0,
                WT_RESULT,
                WT_COMMAND,
                WT_RESTART,
                WT_SPEED,
                WT_DATARESULT,
                WT_HIGH,
            };

        public:
            PLCTask(QObject* parent = nullptr);

            ~PLCTask();

        public:
            void init();
            void exit();

        public:
            void run();
            void quit();
            void start();

        public:
            void setHeartbeatEnable(bool enable);
            void writeResult(const int& result);
            void writeResult(DataRow data);
            void setDetectionState(const int& state);
            bool readSpecName(QString& name);
            bool readParams(ProductParams& params);
        private:
            void writePlc(QSharedPointer<Util::PLC> device, QList<int> lstType, QList<int> lstStartPos, Core::Values lstValues);

            void readPlc(QSharedPointer<Util::PLC> device, int beginPos, int counts, QList<uint16_t>& slstValue, int type = 2);
            void readPlc(QSharedPointer<Util::PLC> device, int beginPos, int counts, QList<int>& slstValue, int type = 3);
            void readPlc(QSharedPointer<Util::PLC> device, int beginPos, int counts, QString& str, int type = 9);
            QString qsintToASCIIString(int);
        public slots:
           void onHeartTimeout();
        public:
            static PLCTask* instance();

        private:
            OI_DISABLE_COPY(PLCTask);
            OI_DECLARE_IMPL(PLCTask);

        };
    }
}


#endif